package programme;

public class SyncThreads1 implements Runnable {

	boolean alive;
	int i;

	@Override
	public void run() {
		System.out.println("Thread starten...");

		i = 0;
		alive = true;

		while (true) {
			synchronized (this) {
				if (alive) {
					i++;
				} else {
					break;
				}
			}
		}
		System.out.println("Thread stoppet!");
	}

	public static void main(String[] args) {
		SyncThreads1 c = new SyncThreads1();

		new Thread(c).start();

		while (Thread.activeCount() > 1) {

			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			long iNow;

			synchronized (c) {
				iNow = c.i;
			}
			System.out.println(iNow);

			if (iNow >= LIMIT) {
				synchronized (c) {
					c.alive = false;
				}
				System.out
						.println("Thread gestoppt! (alive = " + c.alive + ")");
			}
		}

	}

	private static final long LIMIT = 100000000L;
}
